package jmine.tec.batch.services;

import jmine.tec.batch.domain.MPhase;
import jmine.tec.batch.export.MPhaseDependencyAssociation;
import jmine.tec.services.annotations.Execution;
import jmine.tec.services.annotations.Input;
import jmine.tec.services.annotations.ServiceImplementor;
import jmine.tec.services.api.ActionsEnum;
import jmine.tec.services.api.export.ReferenceMap;
import jmine.tec.services.api.export.ServiceFiller;
import jmine.tec.services.io.ServiceBean;

/**
 * Serviço que adiciona uma nova relação de dependencia entre {@link MPhase}.
 * 
 * @author Rafael Volpato
 * @date Oct 30, 2009
 */
@ServiceImplementor(action = ActionsEnum.INCLUIR)
public class MPhaseDependencyService implements ServiceFiller<MPhaseDependencyAssociation> {

    private static final String MPHASE = "Fase";

    private static final String MPHASE_DEPENDENCY = "Depende de";

    private static final String SERVICE_NAME = "MPhaseDependency";

    private MPhase phase;

    private MPhase phaseDependency;

    /**
     * @param phase the phase to set
     */
    @Input(fieldName = MPHASE)
    public void setPhase(MPhase phase) {
        this.phase = phase;
    }

    /**
     * @param phaseDependency the phaseDependency to set
     */
    @Input(fieldName = MPHASE_DEPENDENCY)
    public void setPhaseDependency(MPhase phaseDependency) {
        this.phaseDependency = phaseDependency;
    }

    /**
     * Adiciona a phaseDependency como sendo uma dependencia da phase, ou se ja a phase soh pode ser executada se a phaseDependency ja foi
     * executada com sucesso.
     */
    @Execution
    public void execute() {
        this.phase.addDependency(this.phaseDependency);
        this.phase.getPersister().save();
    }

    /**
     * {@inheritDoc}
     */
    public void fillServiceBean(ServiceBean bean, ReferenceMap referenceMap, MPhaseDependencyAssociation entity) {
        bean.setAction(ActionsEnum.INCLUIR);
        bean.setName(SERVICE_NAME);

        bean.putParameter(MPHASE, entity.getBaseEntity().getMnemonico());
        bean.putParameter(MPHASE_DEPENDENCY, entity.getAssociatedEntity().getMnemonico());
    }
}
